﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Xml.Serialization;
using System.ComponentModel;
using Ppt = Microsoft.Office.Interop.PowerPoint;
using ObjectModel.PowerPoint.RulesModel;

namespace ObjectModel.PowerPoint.Old
{
    public class LinkedObjectCollection : ObservableCollection<LinkedObject>, INotifyPropertyChanged
    {
        LinkedObject mSelectedItem;

        //[XmlIgnore]
        //public LinkedObject SelectedItem
        //{
        //    get { return mSelectedItem; }
        //    set
        //    {
        //        mSelectedItem = value;
        //        OnPropertyChanged(new PropertyChangedEventArgs("SelectedItem"));
        //    }
        //}
        
        public void UpdateAll()
        {
            foreach (LinkedObject target in this)
                target.UpdateValue();
        }

        protected override void OnCollectionChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            base.OnCollectionChanged(e);

            switch (e.Action)
            {
                case System.Collections.Specialized.NotifyCollectionChangedAction.Add:
                    foreach (object o in e.NewItems)
                        ((LinkedObject)o).Target.Deleted += Target_Deleted;
                    break;

                case System.Collections.Specialized.NotifyCollectionChangedAction.Remove:
                    foreach (object o in e.OldItems)
                        ((LinkedObject)o).Target.Deleted -= Target_Deleted;
                    break;
            }
        }

        void Target_Deleted(object sender, EventArgs e)
        {            
            LinkedObject itemToRemove = this.First(o => o.Target == sender);
            Remove(itemToRemove);
        }

        //void Target_SelectedExclusively(object sender, EventArgs e)
        //{
        //    LinkedObject selItem = this.First(o => o.Target == sender);

        //    SelectedItem = selItem;
        //}
    }
}
